﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Acme.AuditExample.EFDAL;
using Acme.AuditExample.EFDAL.Entity;
using Acme.AuditExample.EFDAL.Audit;

namespace AuditConsole
{
	class EFDALExample
	{
		public static void Run()
		{
			Guid createdCustomerID = Guid.Empty;

			// Add a couple of customer objects. You will notice that during the creation of the ObjectContext
			// (AuditExampleEntities). We provide a context startup object that specifies the modifying user
			// We are also not setting modified_by, modified_on, created_by or created_on fields. These are 
			// implemented by the framework.
			ContextStartup user14Startup = new ContextStartup("User14");
			using (AuditExampleEntities context = new AuditExampleEntities(user14Startup))
			{
				//Create a simple customer
				//When persisted create record will be added to the audit table 
				Customer simpleCustomer = new Customer();
				simpleCustomer.Name = "Simple Customer";
				context.AddItem(simpleCustomer);

				//Create another customer
				//When persisted Create record will be added to the audit table
				Customer customer = new Customer();
				customer.Name = "Test Name 1";
				context.AddItem(customer);

				//Persist both customers they will both have the modifier or User14
				context.SaveChanges();

				//Update the name. Updated record will be added to audit table
				customer.Name = "Test Name 2";
				context.SaveChanges();

				createdCustomerID = customer.CustomerId;
			}

			//Lets look at what the create a modify produced
			using (AuditExampleEntities context = new AuditExampleEntities(user14Startup))
			{
				//Retrieve customer from database that we just saved.
				Customer customer = context.Customer.
					Single(cust => cust.CustomerId == createdCustomerID);

				//Write Audit Records. There will be two records. 
				//The first record will represent the creation.
				//The second record will represent the modification.
				foreach (CustomerAudit customerAudit in customer.GetAuditRecords())
				{
					Console.WriteLine("AuditDate: " + customerAudit.AuditDate.ToString());
					Console.WriteLine("AuditType: " + customerAudit.AuditType.ToString());
					Console.WriteLine("CustomerId: " + customerAudit.CustomerId.ToString());
					Console.WriteLine("Name: " + customerAudit.Name);
					Console.WriteLine("ModifiedBy: " + customerAudit.ModifiedBy);
				}
			}

			//Modify the recored one more times and delete
			//We can then go to the database to view the full audit record
			ContextStartup user15Startup = new ContextStartup("User15");
			using (AuditExampleEntities context = new AuditExampleEntities(user15Startup))
			{
				Customer retrievedCustomer = context.Customer.Single(x => x.CustomerId == createdCustomerID);

				//Update the name. Updated record will be added to audit table
				retrievedCustomer.Name = "Test Name 3";
				context.SaveChanges();

				//Delete the customer. Delte recored will be added to the audit table
				context.DeleteObject(retrievedCustomer);
				context.SaveChanges();

				//Write audit records there will be four
				//The first record will represent the creation.
				//The second record will represent the modification from above section.
				//The third record will represent the modification from this section.
				//The fourth record will represent the delete.
				foreach (CustomerAudit customerAudit in retrievedCustomer.GetAuditRecords())
				{
					Console.WriteLine("AuditDate: " + customerAudit.AuditDate.ToString());
					Console.WriteLine("AuditType: " + customerAudit.AuditType.ToString());
					Console.WriteLine("CustomerId: " + customerAudit.CustomerId.ToString());
					Console.WriteLine("Name: " + customerAudit.Name);
					Console.WriteLine("ModifiedBy: " + customerAudit.ModifiedBy);
				}
			}

		}
	}
}
